﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WinFrameApp
{
    public class UserControlBehaviors: Microsoft.Xaml.Behaviors.Behavior<FrameworkElement>
    {
        //  Message：外部需要嵌套Grid
        Grid parent;   
        // Zoom控件
        StiZoomableCanvas zoomable;
               
        void RefreshLocation()
        {
            if (this.zoomable == null) return;
          
            var width = this.zoomable.ActualWidth - AssociatedObject.ActualWidth;
            var height = this.zoomable.ActualHeight - this.AssociatedObject.ActualHeight;
            zoomable.Offset = new Point(0- width,0- height);
          
        }


        protected override void OnAttached()
        {
            parent = AssociatedObject.Parent as Grid;

            parent.Children.Remove(AssociatedObject);

            zoomable = new StiZoomableCanvas();

            zoomable.Children.Add(AssociatedObject);

            zoomable.Loaded += Zoomable_Loaded;

            parent.Children.Add(zoomable);

            zoomable.MouseWheel += OnMouseWheel;
            zoomable.MouseMove += OnMouseMove;
            zoomable.MouseLeftButtonDown += Zoomable_MouseLeftButtonDown;
            zoomable.MouseLeftButtonUp += Zoomable_MouseLeftButtonUp;
            

        }

        private void Zoomable_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            zoomable.SetValue(Panel.ZIndexProperty, 0);
            //zoomable.MouseMove -= OnMouseMove;
        }

        private void Zoomable_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
           
            zoomable.SetValue(Panel.ZIndexProperty,999);
        }

        private void Zoomable_Loaded(object sender, RoutedEventArgs e)
        {
            this.RefreshLocation();
        }

        private void OnMouseWheel(object sender, MouseWheelEventArgs e)
        {

            //var x = Math.Pow(2, e.Delta / 3.0 / Mouse.MouseWheelDeltaForOneLine);
            //zoomable.Scale *= x;

            //// Adjust the offset to make the point under the mouse stay still.
            //var position = (Vector)e.GetPosition(parent);
            //zoomable.Offset = (Point)((Vector)(zoomable.Offset + position) * x - position);

            e.Handled = true;
        }

        private Point LastMousePosition;

        private void OnMouseMove(object sender, MouseEventArgs e)
        {           
            var position = e.GetPosition(this.parent);

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                               
                Vector m=position - LastMousePosition;
               
                Console.WriteLine($"Offset:{zoomable.Offset.X} Box:{zoomable.Extent.X}");

                this.zoomable.Offset -= m;

                if (zoomable.Extent.X - zoomable.Offset.X <= 0)
                {                    
                    zoomable.Offset = new Point(zoomable.Extent.X, zoomable.Offset.Y);
                }

                if (zoomable.Extent.Y - zoomable.Offset.Y <= 0)
                {                    
                    zoomable.Offset = new Point(zoomable.Offset.X, zoomable.Extent.Y);
                }

                double n=AssociatedObject.ActualWidth - zoomable.Offset.X+ zoomable.Extent.X;
                if (n > zoomable.ActualWidth)
                {
                    double s=0- zoomable.ActualWidth + zoomable.Extent.X+ AssociatedObject.ActualWidth;
                    zoomable.Offset = new Point(s, zoomable.Offset.Y);
                }

                double n1 = AssociatedObject.ActualHeight - zoomable.Offset.Y + zoomable.Extent.Y;
                if (n1 > zoomable.ActualHeight)
                {
                    double s = 0 - zoomable.ActualHeight + zoomable.Extent.Y + AssociatedObject.ActualHeight;
                    zoomable.Offset = new Point(zoomable.Offset.X,s);
                }


                Console.WriteLine($"N:{n}");
            }



            LastMousePosition = position;
        }



    }
}
